tmr = Timer.new()
tmr:start()
clock ={}

testimage = Image.load("testclasses/resources/default.png", VRAM)
testimagegif = Image.load("testclasses/resources/point.gif", VRAM)
testsprite = Sprite.new ("testclasses/resources/char.png", 24,32,VRAM)
testsprite:addAnimation( {0,1,2,1}, 300 ) -- 1

-- load image for every single object when created
function imageloadevery_ds(o)
  local ram = VRAM
  -- element images/sprites load
  local paintable = {o} --clone(paintable_objects)
    for k,v in ipairs(o.states) do
      table.insert(paintable, v)
    end
  local directions = {'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest'}
  
  for k in ipairs(paintable) do
  	-- TODO: rawget (-> ignores metatable, no inheritance)? or no image/sprite for gameelement?
     
	  paintable[k].paint = {}

  	if paintable[k].sprite then
      --paintable[k] = nil
      --TODO: dynamisch mache ...
			paintable[k].paint.sprite = Sprite.new (paintable[k].sprite.image, paintable[k].sprite.wid, paintable[k].sprite.hig,ram)
	    paintable[k].paint.sprite:addAnimation( {0,1,2,1}, 300 ) -- 1
	    paintable[k].paint.sprite:addAnimation( {0,1,2,1}, 300 ) -- 5
	    paintable[k].paint.sprite:addAnimation( {3,4,5,4}, 300 ) -- 2
	    paintable[k].paint.sprite:addAnimation( {6,7,8,7}, 300 ) -- 6
	    paintable[k].paint.sprite:addAnimation( {6,7,8,7}, 300 ) -- 3
	    paintable[k].paint.sprite:addAnimation( {6,7,8,7}, 300 ) -- 7
	    paintable[k].paint.sprite:addAnimation( {9,10,11,10}, 300 ) -- 4
	    paintable[k].paint.sprite:addAnimation( {0,1,2,1}, 300 ) -- 8	
    elseif paintable[k].image then
      paintable[k].paint.image = Image.load(paintable[k].image.default, ram)
      for i,v in ipairs(directions) do
        if paintable[k].image[v] then
  	    	paintable[k].paint[v] = Image.load(paintable[k].image[v], ram)
				end
			end
  	else -- if no image and no sprite is set
  	--TODO: do nothing?
  	end
	end
end

function testpaint()
  screen.blit(SCREEN_DOWN, hero.x + math.random(-40,40), hero.y + math.random(-40,40), testimagegif)
end

function paint_ds(o)
if o.currentstate.paint then
  if o.currentstate.paint.sprite then
  	-- TODO: check if dirs avaible, else default
    if o.dir == 'north' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 0)
  	elseif o.dir == 'northeast' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 1)
  	elseif o.dir == 'east' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 2)
  	elseif o.dir == 'southeast' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 3)
  	elseif o.dir == 'south' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 4)
  	elseif o.dir == 'southwest' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 5)
  	elseif o.dir == 'west' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 6)
  	elseif o.dir == 'northwest' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 7)
  end 
	screen.print(SCREEN_DOWN, o.x, o.y, 'cs') 
  elseif o.currentstate.paint.image then
  screen.blit(SCREEN_DOWN, o.x, o.y, o.currentstate.paint.image.default)
  screen.print(SCREEN_DOWN, o.x, o.y, 'ci') 
  end
elseif o.paint then
  if o.paint.sprite then
    -- testsprite:playAnimation(SCREEN_DOWN, o.x + 9, o.y + 9, 1)
  	if o.dir == 'north' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 1)
  	elseif o.dir == 'northeast' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 2)
  	elseif o.dir == 'east' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 3)
  	elseif o.dir == 'southeast' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 4)
  	elseif o.dir == 'south' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 5)
  	elseif o.dir == 'southwest' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 6)
  	elseif o.dir == 'west' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 7)
  	elseif o.dir == 'northwest' then o.paint.sprite:playAnimation(SCREEN_DOWN, o.x, o.y, 8)
    end 
	screen.print(SCREEN_DOWN, o.x, o.y, 'ds') 
  elseif o.paint.image then
    screen.blit(SCREEN_DOWN, o.x, o.y, o.paint.image)
    -- TODO: directions
    screen.print(SCREEN_DOWN, o.x, o.y, 'di') 
  else
  -- TODO: if no pic ?

    testsprite:playAnimation(SCREEN_DOWN, o.x + 9, o.y + 9, 1)
  end
end
end

function clock.run()
	clock.time = tmr:time()
	clock.hour = math.floor(clock.time/3600000)
	clock.rest = clock.time % 3600000
	clock.minut = math.floor(clock.time/60000)
	clock.rest = clock.rest % 60000
	clock.second = math.floor(clock.rest/1000)
	clock.rest = clock.rest % 1000
	clock.milli = clock.rest
end

function controlsupdate_ds()
	Controls.read()
  
  --TODO: add Stylus controls
  -- buttons: A,B,X,Y,L,R,UP,DOWN,LEFT,RIGHT,Start,Select
  -- states: held, pressed, released
  if Keys.released.A then
  controls.button1 = 'released'
  end
  if Keys.released.B then
  controls.button2 = 'released'
  end
  if Keys.released.X then
  controls.button3 = 'released'
  end
  if Keys.released.Y then
  controls.button4 = 'released'
  end
  if Keys.released.L then
  controls.button5 = 'released'
  end
  if Keys.released.R then
  controls.button6 = 'released'
  end
  if Keys.released.Right then
  controls.right = 'released'
  end
  if Keys.released.Left then
  controls.left = 'released'
  end
  if Keys.released.Down then
  controls.down = 'released'
  end
  if Keys.released.Up then
  controls.up = 'released'
  end
  if Keys.newPress.A then
  controls.button1 = 'pressed'
  end
  if Keys.newPress.B then
  controls.button2 = 'pressed'
  end
  if Keys.newPress.X then
  controls.button3 = 'pressed'
  end
  if Keys.newPress.Y then
  controls.button4 = 'pressed'
  end
  if Keys.newPress.L then
  controls.button5 = 'pressed'
  end
  if Keys.newPress.R then
  controls.button6 = 'pressed'
  end
  if Keys.newPress.Right then
  controls.right = 'pressed'
  end
  if Keys.newPress.Left then
  controls.left = 'pressed'
  end
  if Keys.newPress.Down then
  controls.down = 'pressed'
  end
  if Keys.newPress.Up then
  controls.up = 'pressed'
  end
  if Keys.held.A then
  controls.button1 = 'held'
  end
  if Keys.held.B then
  controls.button2 = 'held'
  end
  if Keys.held.X then
  controls.button3 = 'held'
  end
  if Keys.held.Y then
  controls.button4 = 'held'
  end
  if Keys.held.L then
  controls.button5 = 'held'
  end
  if Keys.held.R then
  controls.button6 = 'held'
  end
  if Keys.held.Right then
  controls.right = 'held'
  end
  if Keys.held.Left then
  controls.left = 'held'
  end
  if Keys.held.Down then
  controls.down = 'held'
  end
  if Keys.held.Up then
  controls.up = 'held'
  end
end


function paint_ds_layer1() 
    screen.blit(SCREEN_DOWN,0,0, background)
end

function paint_ds_layer3() 
	screen.print(SCREEN_UP, 0,30, "Leben :"..hero.hp)
	screen.print(SCREEN_UP, 0,50, "Mana: "..hero.mana)
	screen.print(SCREEN_UP, 0,110, "X: "..hero.x)
	screen.print(SCREEN_UP, 0,130, "Y: "..hero.y)
	screen.print(SCREEN_UP, 0,90, NB_FPS)
	screen.print(SCREEN_UP, 0,150, hero.currentstate.name)
	screen.print(SCREEN_UP, 70,10, '#'..#gameelements) 
	screen.print(SCREEN_UP, 70,30, 'A'..controls.button1) 
	screen.print(SCREEN_UP, 70,40, 'B'..controls.button2) 
	screen.print(SCREEN_UP, 70,50, 'X'..controls.button3) 
	screen.print(SCREEN_UP, 70,60, 'Y'..controls.button4) 
	screen.print(SCREEN_UP, 70,70, 'L'..controls.button5) 
	screen.print(SCREEN_UP, 70,80, 'R'..controls.button6) 
	screen.print(SCREEN_UP, 70,90, '<-'..controls.left) 
	screen.print(SCREEN_UP, 70,100, '->'..controls.right) 
	screen.print(SCREEN_UP, 70,110, '/\\ '..controls.up) 
	screen.print(SCREEN_UP, 70,120, '\\/'..controls.down) 
	--screen.print(SCREEN_UP, 0,100, hero.save.status.life)

end

function imageload_ds()
  -- background
  local ram
  if math.random(0,1) == 1 then
  ram = VRAM
  else
  ram = RAM
  end
  ram = VRAM
  background = Image.load("testclasses/resources/background.png", ram)
  -- element images/sprites load
  local paintable = paintable_objects --clone(paintable_objects)
  for element_k in ipairs(paintable_objects) do
    for k,v in ipairs(paintable_objects[element_k].states) do
      table.insert(paintable, v)
    end
  end
    local directions = {'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest'}
  
  for k in ipairs(paintable) do
  	-- TODO: rawget (-> ignores metatable, no inheritance)? or no image/sprite for gameelement?
     
	  paintable[k].paint = {}

  	if paintable[k].sprite then
      --paintable[k] = nil
      --TODO: dynamisch mache ...
			paintable[k].paint.sprite = Sprite.new (paintable[k].sprite.image, paintable[k].sprite.wid, paintable[k].sprite.hig,ram)
	    paintable[k].paint.sprite:addAnimation( {0,1,2,1}, 300 ) -- 1
	    paintable[k].paint.sprite:addAnimation( {0,1,2,1}, 300 ) -- 5
	    paintable[k].paint.sprite:addAnimation( {3,4,5,4}, 300 ) -- 2
	    paintable[k].paint.sprite:addAnimation( {6,7,8,7}, 300 ) -- 6
	    paintable[k].paint.sprite:addAnimation( {6,7,8,7}, 300 ) -- 3
	    paintable[k].paint.sprite:addAnimation( {6,7,8,7}, 300 ) -- 7
	    paintable[k].paint.sprite:addAnimation( {9,10,11,10}, 300 ) -- 4
	    paintable[k].paint.sprite:addAnimation( {0,1,2,1}, 300 ) -- 8	
    elseif paintable[k].image then
      paintable[k].paint.image = Image.load(paintable[k].image.default, ram)
      for i,v in ipairs(directions) do
        if paintable[k].image[v] then
  	    	paintable[k].paint[v] = Image.load(paintable[k].image[v], ram)
				end
			end
  	else -- if no image and no sprite is set
  	--TODO: do nothing?
  	end
	end
end

function paint_oht(o)
  if o.oht.status == 1 then
		screen.print(SCREEN_DOWN,o.x,o.y - 10,o.oht.text)
  end
end
